﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client_Vkontakte
{
	public partial class MainForm : Form
	{

		private string _appId = System.Configuration.ConfigurationManager.AppSettings.GetValues("AppID")[0];

		/// <summary>
		/// Extended permissions to ask the user.
		/// </summary>
		/// <remarks>
		/// For extensive list of available extended permissions refer to 
		/// http://vk.com/developers.php?oid=-1&p=%D0%9F%D1%80%D0%B0%D0%B2%D0%B0_%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0_%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B9
		/// </remarks>
		private int _scope;

		public MainForm()
		{
			// Initialize extended permissions variable
			this._scope = (int)(VK.Lib.VkontakteScopeList.notify | VK.Lib.VkontakteScopeList.friends | VK.Lib.VkontakteScopeList.photos |
								VK.Lib.VkontakteScopeList.audio | VK.Lib.VkontakteScopeList.video | VK.Lib.VkontakteScopeList.offers |
								VK.Lib.VkontakteScopeList.questions | VK.Lib.VkontakteScopeList.pages | VK.Lib.VkontakteScopeList.link |
								VK.Lib.VkontakteScopeList.speedWallPublish | VK.Lib.VkontakteScopeList.status | VK.Lib.VkontakteScopeList.notes |
								VK.Lib.VkontakteScopeList.messages | VK.Lib.VkontakteScopeList.wall | VK.Lib.VkontakteScopeList.advertisingOffice |
								VK.Lib.VkontakteScopeList.docs | VK.Lib.VkontakteScopeList.usersGroups | VK.Lib.VkontakteScopeList.usersNotifications);

			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			// open the VK Login Dialog and ask for user permissions.
			var vkLoginDlg = new VKLoginDialog(_appId, _scope.ToString());
			vkLoginDlg.ShowDialog();

			// The user has taken action, either allowed/denied or cancelled the authorization,
			// which can be known by looking at the dialogs FacebookOAuthResult property.
			// Depending on the result take appropriate actions.
			this.TakeLoggedInAction(vkLoginDlg.VKOAuthResult);
		}

		//https://api.vkontakte.ru/method/getProfiles.xml?uid=9906619&access_token=101f37a15fb8304810d68784f410a291e411089108a27e45c730b602f72686e

		//// open the VK Login Dialog and ask for user permissions.
		//var vkLoginDlg = new VKLoginDialog(_appId, _scope.ToString());
		//vkLoginDlg.ShowDialog();

		//// The user has taken action, either allowed/denied or cancelled the authorization,
		//// which can be known by looking at the dialogs FacebookOAuthResult property.
		//// Depending on the result take appropriate actions.
		//this.TakeLoggedInAction(vkLoginDlg.VKOAuthResult);












		private void TakeLoggedInAction(VK.Lib.VKOAuthResult vkOAuthResult)
		{
			if (vkOAuthResult == null)
			{
				// the user closed the FacebookLoginDialog, so do nothing.
				MessageBox.Show("Cancelled!");
				return;
			}

			// Even though facebookOAuthResult is not null, it could had been an 
			// OAuth 2.0 error, so make sure to check IsSuccess property always.
			if (vkOAuthResult.IsSuccess)
			{
				// since our respone_type in FacebookLoginDialog was token,
				// we got the access_token
				// The user now has successfully granted permission to our app.
				//var dlg = new FacebookInfoDialog(vkOAuthResult.AccessToken);
				//dlg.ShowDialog();
				MessageBox.Show(@"OK");
			}
			else
			{
				// for some reason we failed to get the access token.
				// most likely the user clicked don't allow.
				MessageBox.Show(vkOAuthResult.ErrorDescription);
			}
		}

	}

}